package edu.uj.petri.controller;

import edu.uj.petri.commands.DiagramElementMoveCommand;
import edu.uj.petri.commands.Memento;
import edu.uj.petri.commands.RemoveArcCommand;
import edu.uj.petri.commands.RemovePlaceCommand;
import edu.uj.petri.commands.RemoveTransitionCommand;
import edu.uj.petri.document.Document;
import edu.uj.petri.view.ArcView;
import edu.uj.petri.view.ContextMenu;
import edu.uj.petri.view.DiagramElement;
import edu.uj.petri.view.PlaceView;
import edu.uj.petri.view.TransitionView;
import edu.uj.petri.view.View;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;

public class SelectEventHandler implements EventHandler {

    private boolean dragging = false;
    private Point2D moveStart, moveEnded;
    private DiagramElement selected;
    private Memento initialPositionMemento;
    
    public SelectEventHandler()
    {
        if (Document.getActiveDocument() != null) Document.getActiveDocument().getView().selectNone();
        if (NewInhibitorArcEventHandler.unfinished != null) 
        {
            Document.getActiveDocument().getView().removeElement(NewInhibitorArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewInhibitorArcEventHandler.unfinished = null;
            
        } else if (NewNormalArcEventHandler.unfinished != null)  
        {
            Document.getActiveDocument().getView().removeElement(NewNormalArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewNormalArcEventHandler.unfinished = null;
        }
    }
    
    public void click(double x, double y) {
        
    }

    public void doubleClick(double x, double y) {        
               
        DiagramElement selected = Document.getActiveDocument().getView().getClickedElement( new Point2D.Double( x, y ), false);
        if (selected != null) 
        {
            selected.doubleClick( new Point.Double( x, y ) );
            Document.getActiveDocument().getView().draw();
        }
    }

    public void mouseMove(double x1, double y1, int button) {
        
        /*if (dragging) 
            dragging = (button == MouseEvent.BUTTON1);
        
        if (selected != null)
            if (dragging) 
        {
            Point2D newPoint = new Point2D.Double( x1, y1 );
            selected.move( newPoint, moveEnded );
            Document.getActiveDocument().getView().draw();
            moveEnded = newPoint;
        }*/
    }

    public void mouseButtonDown(double x, double y) {
        View view = Document.getActiveDocument().getView();
        selected = view.getClickedElement( new Point2D.Double( x, y ), false);
        if (selected == null) {
            view.selectNone();
        } else {
            dragging = true;
            initialPositionMemento = selected.getMemento();
            moveStart = new Point.Double( x, y );
            moveEnded = moveStart;
            view.clickSelectableElement( selected );
            view.draw();
        }
    }

    public void mouseButtonUp(double x, double y) {
        
        dragging = false;
        if (selected != null) {
            DiagramElementMoveCommand c = new DiagramElementMoveCommand(selected, initialPositionMemento);
            Document.getActiveDocument().execute(c);
        }
    }

    public void rightClick(double x, double y) {
        
        DiagramElement selected = Document.getActiveDocument().getView().getClickedElement( new Point2D.Double( x, y ), false);
        if (selected != null) {
            
            Point point = Document.getActiveDocument().getView().getCanvas().convertCanvasToScreen(x,y);
            if (selected.getContextMenu() != null)
            {
                if (selected instanceof ArcView)
                {
                    
                    ContextMenu menu = ((ArcView)(selected)).getMenu(point);
                    menu.show( Document.getActiveDocument().getView().getCanvas().getComponent(), (int)point.getX(), (int)point.getY());
                    Document.getActiveDocument().getView().draw();  
                    return;
                }
                
                selected.getContextMenu().show( Document.getActiveDocument().getView().getCanvas().getComponent(), (int)point.getX(), (int)point.getY());
            }
            
            Document.getActiveDocument().getView().draw();  
            
        }
        
    }
    

    /**
     * handles Escape and Delete
     */
    public void keyPressed(int keyCode) {
        
       if (keyCode == KeyEvent.VK_DELETE) 
       {
           if (selected != null) 
           {
               if (selected instanceof TransitionView) Document.getActiveDocument().execute(
                       new RemoveTransitionCommand( (TransitionView)selected ));
               
               if (selected instanceof PlaceView) Document.getActiveDocument().execute(
                       new RemovePlaceCommand( (PlaceView)selected ));
               
               if (selected instanceof ArcView) Document.getActiveDocument().execute(
                       new RemoveArcCommand( (ArcView)selected ));
               
           }
       } 
    }

    public void mouseDragged(double x, double y) {
        
        if (selected != null)
        {
            Point2D newPoint = new Point2D.Double( x, y );
            selected.move( newPoint, moveEnded );
            Document.getActiveDocument().getView().draw();
            moveEnded = newPoint;
        }
    }

}
